/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlparser;

import base.*;
import java.io.*;
/**
 *
 * @author pbelov
 */
public class xmlWriter extends File {
    private static String XMLTITLE =
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
    private static String DOCSTART = "<doc>\n";
    private static String DOCEND = "</doc>";

    private static String TABSPACE = "    ";

    private FileWriter fw;
    private BufferedWriter bw;

    private FileReader fr;
    private BufferedReader br;

    private String fileName;
    public static String DBPATH = "DB" + File.separator;
    
    public xmlWriter(String fileName) {
        super(fileName);
        this.fileName = fileName;
        //openFile();
    }

    private boolean openFile() {
        boolean res = true;
        try {
            if (this.exists() || true) {
                System.out.println("[DBG]Creating file [" + fileName + "]");
                fw = new FileWriter(fileName);
                bw = new BufferedWriter(fw);
            } else {
                System.out.println("[DBG]Reading file [" + fileName + "]");
                fr = new FileReader(fileName);
                br = new BufferedReader(fr);
                
                String s = null;
                
                while( (s = br.readLine()) != null) {
                    System.out.println("> " + s);
                }
            }
        } catch (IOException ex) {
            res = false;
        }
        return res;
    }


    private void closeFile() throws IOException {
        if (bw != null) bw.close();
        if (br != null) br.close();
    }
    
    public boolean createFile() throws IOException {
        boolean res = false;
        File bugFile = new File(fileName);
        if (!bugFile.exists()) {
            System.out.println("[DBG]Creating file: " + fileName);
            res = bugFile.createNewFile();
        }

        fw = new FileWriter(fileName);
        bw = new BufferedWriter(fw);

        return res;
    }

    public void createFile(Bug bug) throws IOException {
        if (openFile() == true) {
            bw.write(XMLTITLE);
            bw.write(DOCSTART);
            if (bug != null) {
                String s = null;
                s = getFormattedString(bugParser.ID, Integer.toString(bug.getID()));
                bw.write(s);
                s = getFormattedString(bugParser.TITLE, bug.getTitle());
                bw.write(s);
                s = getFormattedString(bugParser.DESC, bug.getDescription());
                bw.write(s);
                s = getFormattedString(bugParser.ASSIGNEE, bug.getAssignee());
                bw.write(s);
                s = getFormattedString(bugParser.CREATOR, bug.getCreator());
                bw.write(s);
                s = getFormattedString(bugParser.TESTER, bug.getAssignee());
                bw.write(s);
                s = getFormattedString(bugParser.DEVICE, bug.getProject());
                bw.write(s);
                s = getFormattedString(bugParser.PROJECT, bug.getProject());
                bw.write(s);
                s = getFormattedString(bugParser.PRIRITY, Integer.toString(bug.getPriority()));
                bw.write(s);
                s = getFormattedString(bugParser.STATE, Integer.toString(bug.getState()));
                bw.write(s);
                s = getFormattedString(bugParser.BUGVER, bug.getBugVer());
                bw.write(s);
                s = getFormattedString(bugParser.FIXVER, bug.getFixVer());
                bw.write(s);
                s = getFormattedString(bugParser.FIX, Integer.toString(bug.getFixNumber()));
                bw.write(s);
                s = getFormattedString(bugParser.REOPEN, Integer.toString(bug.getReopenCount()));
                bw.write(s);

                /* Commented temporary. TODO: needs re-implement this
                for( int i = 0; i < bug.getCommentsCount(); i++) {
                    s = getFormattedString(bugParser.COMMENT, bug.getComment(i));
                    bw.write(s);
                }
                 */
            }

            bw.write(DOCEND);
            closeFile();
        }
    }

    // Just creating info bug with bug count
    public void createInfoFile(int ID) throws IOException {
        openFile();
        bw.write(XMLTITLE);
        bw.write(DOCSTART);
        String s = getFormattedString(infoParser.LAST_BUG, Integer.toString(ID));
        bw.write(s);
        bw.write(DOCEND);
        closeFile();
    }

    private int getoffset(String attr) {
        int offset = 0;

        return offset;
    }

    private String getFormattedString(String attr, String value) {
        StringBuffer res = new StringBuffer();

        res.append(TABSPACE);
        res.append("<");
        res.append(attr);
        res.append(">");
        res.append(value);
        res.append("</");
        res.append(attr);
        res.append(">");
        res.append("\n");

        return res.toString();
    }

    public boolean changeAttr(String attr, String value) {
        boolean res = true;
        
        return res;
    }
}